/**
 * 
 */
package tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.LinkedList;

import main.PrimsGraph;

import org.junit.Before;
import org.junit.Test;

import startercode.Edge;
import startercode.GraphInput;
import startercode.SimpleGraph;

/**
 * @author Tony Butterfield
 *
 */
public class PrimsGraphTest
{
  SimpleGraph testGraph6a = new SimpleGraph();
  PrimsGraph mst6a;
  LinkedList<Edge> edges;

  /**
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception
  {    
    GraphInput.LoadSimpleGraph(testGraph6a, "src/main/testgraph6a.txt");
    mst6a = new PrimsGraph(testGraph6a);
    edges = mst6a.getEdges();
    mst6a.displayEdges();
  }

  /** test to see that PrimsGraph is returning the correct number of edges */
  @Test
  public final void testNumberEdges()
  {
    assertEquals("the MST returned on testgraph6a did not have the correct (|V|- 1) number of edges ", 5, edges.size());
  }
  
  /** test to see that PrimsGraph is returning the correct edges */
  @Test
  public final void testEdges()
  {        
    assertTrue("the MST returned on testgraph6a did not contain edge (a,b)", testEdge("a","b",3.0));
    assertTrue("the MST returned on testgraph6a did not contain edge (b,c)", testEdge("b","c",1.0));
    assertTrue("the MST returned on testgraph6a did not contain edge (b,f)", testEdge("b","f",4.0));
    assertTrue("the MST returned on testgraph6a did not contain edge (f,e)", testEdge("f","e",2.0));
    assertTrue("the MST returned on testgraph6a did not contain edge (f,d)", testEdge("f","d",5.0));    
  }
  
  /** test to see that the MST returned by PrimsGraph has the correct total weight */
  @Test
  public final void testWeight()
  {
    assertTrue("the MST returned on testgraph6a does not have the correct weight", mst6a.getWeight().equals(15.0));
  }
  
  private boolean testEdge(String v1, String v2, Double data)
  {    
    boolean result = false;
    
    for (Edge e : edges)
    {
      String e1 = (String) e.getFirstEndpoint().getName();
      String e2 = (String) e.getSecondEndpoint().getName();
      
      if (e1.equals(v1) && e2.equals(v2) && data.equals(e.getData()))
      {
        result = true;
        break;
      }
    }
    
    return result;
  }

}
